package org.mspring.platform.dao.query.criterion.inparam;

import java.util.Collection;

/**
 * @author Gao Youbo
 * @since Jan 31, 2012
 */
public class CollectionMarshaller implements Marshaller {

    private Collection<?> values;

    public CollectionMarshaller(Collection<?> value) {
        this.values = value;
    }

    @Override
    public Object getNamedQueryParamValue(Class<?> paramClass) {
        // doesn't check param's type
        return values;
    }

    @Override
    public boolean hasValues() {
        return !values.isEmpty();
    }

    @Override
    public String stringValue() {
        Object[] array = values.toArray();

        StringBuilder buf = new StringBuilder(48);
        for (int i = 0; i < array.length; i++) {
            buf.append(array[i]);
            if (i < array.length - 1) {
                buf.append(",");
            }
        }
        return buf.toString();
    }

    @Override
    public String toString() {
        return "CollectionMarshaller";
    }

}
